#pragma once
#include<iostream>
using namespace std;

enum Colour
{
	RED,
	BLACK
};

template<class T>
struct RBTNode
{
	RBTNode(const T& data)
		:_data(data)
		, _left(nullptr)
		, _right(nullptr)
		, _parent(nullptr)
		, _col(RED)
	{
		
	}

	T _data;
	RBTNode<T>* _left;
	RBTNode<T>* _right;
	RBTNode<T>* _parent;
	Colour _col;
};

template<class T,class Ref,class Ptr>
class RBTiterator
{
	typedef RBTNode<T> Node;
	typedef RBTiterator<T, Ref,Ptr> Self;
public:
	RBTiterator(Node* node,Node* root)
		:_node(node)
		,_root(root)
	{

	}

	Self& operator++()
	{
		if (_node->_right)
		{
			Node* cur = _node->_right;
			while (cur->_left)
			{
				cur = cur->_left;
			}
			_node = cur;
		}
		else
		{
			Node* cur = _node;
			Node* parent = _node->_parent;
			while (parent && parent->_right == cur)
			{
				cur = parent;
				parent = cur->_parent;
			}
			_node = parent;
		}
		return *this;
	}

	Self& operator--()
	{
		if (nullptr == _node)
		{
			Node* cur = _root;
			while (cur->_right)
			{
				cur = cur->_right;
			}
			_node = cur;
		}
		else if (_node->_left)
		{
			Node* cur = _node->_left;
			while (cur->_right)
			{
				cur = cur->_right;
			}
			_node = cur;
		}
		else
		{
			Node* cur = _node;
			Node* parent = cur->_parent;
			while (parent &&parent->_left == cur)
			{
				cur = parent;
				parent = cur->_parent;
			}
			_node = parent;
		}
		return *this;
	}
	
	bool operator==(const Self& s)  const
	{
		return _node == s._node;
	}

	bool operator!=(const Self& s) const
	{
		return _node != s._node;
	}

	Ref operator*()
	{
		return _node->_data;
	}

	Ptr operator->()
	{
		return &_node->_data;
	}

private:
	Node* _node;
	Node* _root;
};

template<class K, class T,class KeyOfT>
class RBTree
{
	typedef RBTNode<T> Node;
public:
	typedef RBTiterator<T, T&, T*> _iterator;
	typedef RBTiterator<T, const T&,const T*> _const_iterator;

	_iterator Begin()
	{
		Node* cur = _root;
		while (cur && cur->_left)
		{
			cur = cur->_left;
		}
		return {cur ,_root};
	}
	
	_iterator End()
	{
		return { nullptr,_root };
	}

	_const_iterator Begin()  const
	{
		Node* cur = _root;
		while (cur && cur->_left)
		{
			cur = cur->_left;
		}
		return { cur,_root };
	}

	_const_iterator End()  const
	{
		return { nullptr,_root };
	}




	void RotateR(Node* parent)
	{
		Node* pparent = parent->_parent;
		Node* subl = parent->_left;
		Node* sublr = subl->_right;
		parent->_left = sublr;
		subl->_right = parent;
		if (sublr)
		{
			sublr->_parent = parent;
		}
		parent->_parent = subl;
		if (pparent)
		{
			if (pparent->_left == parent)
			{
				pparent->_left = subl;
			}
			else
			{
				pparent->_right = subl;
			}
			subl->_parent = pparent;
		}
		else
		{
			_root = subl;
			subl->_parent = nullptr;
		}
	}

	void RotateL(Node* parent)
	{
		Node* pparent = parent->_parent;
		Node* subr = parent->_right;
		Node* subrl = subr->_left;
		parent->_right = subrl;
		subr->_left = parent;
		if (subrl)
		{
			subrl->_parent = parent;
		}
		parent->_parent = subr;
		if (pparent)
		{
			if (pparent->_left == parent)
			{
				pparent->_left = subr;
			}
			else
			{
				pparent->_right = subr;
			}
			subr->_parent = pparent;
		}
		else
		{
			_root = subr;
			subr->_parent = nullptr;
		}
	}

	pair<_iterator,bool> Insert(const T& data)   
	{
		if (nullptr == _root)
		{
			_root = new Node(data);
			_root->_col = BLACK;
			return {_iterator(_root,_root),true};
		}
		Node* cur = _root;
		Node* parent = nullptr;
		KeyOfT kot;
		while (cur)
		{
			if (kot(data) > kot(cur->_data))
			{
				parent = cur;
				cur = cur->_right;
			}
			else if (kot(data) < kot(cur->_data))
			{
				parent = cur;
				cur = cur->_left;
			}
			else
			{
				return {_iterator(cur,_root),false};
			}
		}
		cur = new Node(data);
		Node* newnode = cur;
		if (kot(data) > kot(parent->_data))
		{
			parent->_right = cur;
		}
		else
		{
			parent->_left = cur;
		}
		
		cur->_parent = parent;
		while (parent && parent->_col == RED)
		{
			Node* grandfather = parent->_parent;
			if (grandfather->_left == parent)
			{
				Node* uncle = grandfather->_right;
				if (uncle && uncle->_col == RED)
				{
					uncle->_col = parent->_col = BLACK;
					grandfather->_col = RED;
					cur = grandfather;
					parent = cur->_parent;
				}
				else
				{
					if (parent->_left == cur)
					{
						RotateR(grandfather);
						parent->_col = BLACK;
						grandfather->_col = RED;
						break;
					}
					else
					{
						RotateL(parent);
						RotateR(grandfather);
						cur->_col = BLACK;
						grandfather->_col = RED;
						break;
					}
				}
			}
			else
			{
				Node* uncle = grandfather->_left;
				if (uncle && uncle->_col == RED)
				{
					uncle->_col = parent->_col = BLACK;
					grandfather->_col = RED;
					cur = grandfather;
					parent = cur->_parent;
				}
				else
				{
					if (parent->_right == cur)
					{
						RotateL(grandfather);
						parent->_col = BLACK;
						grandfather->_col = RED;
						break;
					}
					else
					{
						RotateR(parent);
						RotateL(grandfather);
						cur->_col = BLACK;
						grandfather->_col = RED;
						break;
					}
				}
			}
		}
		_root->_col = BLACK;

		return {_iterator(newnode,_root),true};
	}

	
	Node* Find(const K& key)
	{
		Node* cur = _root;
		KeyOfT kot;
		while (cur)
		{
			if (key > kot(cur->_data))
			{
				cur = cur->_right;
			}
			else if (key < kot(cur->_data))
			{
				cur = cur->_left;
			}
			else
			{
				return cur;
			}
		}
		return nullptr;
	}


	int Height()
	{
		return _Height(_root);
	}

	int Size()
	{
		return _Size(_root);
	}

	

private:
	int _Height(Node* root)
	{
		if (nullptr == root)
		{
			return 0;
		}
		int lefth = _Height(root->_left);
		int righth = _Height(root->_right);
		return lefth > righth ? lefth + 1 : righth + 1;
	}

	int _Size(Node* root)
	{
		if (nullptr == root)
		{
			return 0;
		}
		return _Size(root->_left) + _Size(root->_right) + 1;
	}

	Node* _root = nullptr;
};

